package com.mall.user.services;

import com.alibaba.fastjson.JSON;
import com.mall.commons.tool.exception.ValidateException;
import com.mall.user.IVerifyService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dal.entitys.UserVerify;
import com.mall.user.dal.persistence.UserVerifyMapper;
import com.mall.user.dto.UserVerifyRequest;
import com.mall.user.dto.UserVerifyResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * VerifyServiceImpl
 * created by feng on 7/21/21
 */

@Service
@Slf4j
public class VerifyServiceImpl implements IVerifyService {

    @Autowired
    private UserVerifyMapper userVerifyMapper;

    @Override
    public UserVerifyResponse verify(UserVerifyRequest userVerifyRequest) {
        UserVerifyResponse response = new UserVerifyResponse();

        // 判空验证
        userVerifyRequest.requestCheck();

        // 1. 向tb_verify查看uuid与verify相对应的数据
        try {
            Example example = new Example(UserVerify.class);
            // username 和 uuid相同， 同时没有认证
            example.createCriteria()
                    .andEqualTo("username", userVerifyRequest.getUserName())
                    .andEqualTo("uuid", userVerifyRequest.getUuid())
                    .andEqualTo("isVerify", "N");
            List<UserVerify> userList = userVerifyMapper.selectByExample(example);
            if (CollectionUtils.isEmpty(userList)) {
                throw new ValidateException(SysRetCodeConstants.EMAIL_ALREADY_EXISTS.getCode(), SysRetCodeConstants.EMAIL_ALREADY_EXISTS.getMessage());
            }

            // 2. username和uuid相同， 且没有验证
            UserVerify userVerify = userList.get(0);
            userVerify.setIsVerify("Y");
            int affectRow = userVerifyMapper.updateByPrimaryKey(userVerify);

            if (affectRow != 1) {
                throw new ValidateException(SysRetCodeConstants.DB_EXCEPTION.getCode(), SysRetCodeConstants.DB_EXCEPTION.getMessage());
            }

        } catch (ValidateException e) {
            response.setCode(e.getErrorCode());
            response.setMsg(e.getMessage());
            return response;
        }

        // 打印日志
        log.info("用户激活成功, 激活参数为request:{}, {}", JSON.toJSONString(userVerifyRequest), "xxx");
        response.setCode(SysRetCodeConstants.SUCCESS.getCode());
        response.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        return response;
    }

    @Override
    public Boolean ifUserVerified(String userName) {
        Example example = new Example(UserVerify.class);
        example.createCriteria()
                .andEqualTo("username", userName);
        List<UserVerify> userVerifies = userVerifyMapper.selectByExample(example);

        //还是把异常情况排除一下，虽然别的service已经对userName进行过判断了
        if (userVerifies == null || userVerifies.size() == 0) {
            return false;
        }
        UserVerify userVerify = userVerifies.get(0);
        //如果是N，代表没有被激活
        if ("N".equals(userVerify.getIsVerify())) {
            return false;
        }
        //如果是Y，代表被激活
        else if ("Y".equals(userVerify.getIsVerify())) {
            return true;
        }
        //其他特殊情况，比如说null
        else {
            return false;
        }

    }
}